#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.common import exter_attack, execute_cmd, check_command_injection
from common.const import CMDResult, ExecuteResultEnum
from common.common_models import ActionResult
from common.logger import Logger
from db2.comm.db2_cmd import check_os_user
from db2.comm.util.job_decorators import job_exception_decorator
from db2.comm.backup_param_parse import ParameterBackupParse
from db2.comm.util.common_util import Db2CommonUtil
from db2.comm.const import Db2JsonConstant
from db2.backup.single.single_ts_backup import SingleTsBackup
from db2.backup.db2_backup_base import Db2BackupBase

LOGGER = Logger().get_logger(filename="db2.log")


class HaTsBackup(Db2BackupBase):
    def __init__(self, task_name, pid, job_id, sub_job_id, param_dict):
        super().__init__(task_name, pid, job_id, sub_job_id, param_dict)
        self.param_util = ParameterBackupParse(pid, job_id, sub_job_id, param_dict)
        self.backup_handle = SingleTsBackup(task_name, pid, job_id, sub_job_id, param_dict)

    def allow_backup_in_local_node(self):
        LOGGER.info("Start Ha allow_backup_in_local_node")
        user_name = self.param_util.get_database_user_name(Db2JsonConstant.JOB_PROTECTENV_NODES_0_AUTH_AUTHKEY)
        if not check_os_user(user_name):
            LOGGER.error(f"Parameter security verification failed.")
            return Db2CommonUtil.build_action_result(
                code=ExecuteResultEnum.INTERNAL_ERROR.value, message="")
        exec_cmd = f"su - {user_name} -c 'db2level'"
        ret_code, std_out, std_err = execute_cmd(exec_cmd)
        if ret_code != CMDResult.SUCCESS.value:
            LOGGER.error(f"This node is not the primary node.")
            return Db2CommonUtil.build_action_result(
                code=ExecuteResultEnum.INTERNAL_ERROR.value, message="No primary nodes")
        return self.backup_handle.allow_backup_in_local_node()

    def query_job_permission(self):
        return self.backup_handle.query_job_permission()

    def check_backup_job_type(self):
        return self.backup_handle.check_backup_job_type()

    def exec_backup_pre_job(self):
        return self.backup_handle.exec_backup_pre_job()

    @job_exception_decorator(write_progress=True)
    def exec_backup(self):
        return self.backup_handle.exec_backup()

    def report_copy(self, bak_time):
        return self.backup_handle.report_copy(bak_time)

    def query_backup_copy(self):
        pass

    def exec_backup_post_job(self):
        return self.backup_handle.exec_backup_post_job()

    @exter_attack
    def report_progress(self):
        return self.backup_handle.report_progress()

    def output_action_result(self, action_ret: ActionResult):
        super(HaTsBackup, self).output_action_result(action_ret)

    def abort_job(self):
        return self.backup_handle.abort_job()
